﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BoutiquePOS.DAL.DAO;
using BoutiquePOS.Manager;

namespace BoutiquePOS.UI.UserUI
{
    public partial class ProductGenderEntry : System.Web.UI.Page
    {
        private ProductGenderManager aProductGenderManager = new ProductGenderManager();
        private ProductGender aProductGender = new ProductGender();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                LoadGridview();
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void SaveProductGender(object sender, EventArgs e)
        {
            try
            {
                aProductGenderManager = new ProductGenderManager();
                aProductGender.GenderName = genderNameTextBox.Value;
                string message = aProductGenderManager.SaveProductGender(aProductGender);
                messageResult.InnerText = message;
            }
            catch (Exception exception)
            {
                messageResult.InnerText = "Gender name can not be saved" + "\n" + exception.ToString();
            }
        }

        public void CloseButton(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }
        public void ClearButton(object sender, EventArgs e)
        {
            Clear();
        }
        private void Clear()
        {
            genderIdTextBox.Value = "";
            genderNameTextBox.Value = "";
        }

        public void DeleteButton(object sender, EventArgs e)
        {
            try
            {
                int GenderId = Convert.ToInt32(genderIdTextBox.Value);
                ProductGenderManager productGenderManager= new ProductGenderManager();
                string aa = productGenderManager.DeleteProductCategory(GenderId);
                messageResult.InnerText = aa;
            }
            catch (Exception)
            {
                messageResult.InnerText = "Delete Failed";
            }
        }

        private void LoadGridview()
        {
            try
            {
                List<ProductGender> aProductGenders;
                aProductGenders = aProductGenderManager.GetAllProductGender();
                genderGridView.DataSource = aProductGenders;
                genderGridView.DataBind();
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void LoadGridViewOnSaerch(int i, string genderIdorName)
        {
            try
            {
                genderGridView.DataSource = null;
                genderGridView.DataBind();
                List<ProductGender> genderManagers;
                ProductGenderManager genderManager = new ProductGenderManager();
                genderManagers = genderManager.GetAllGenderByIdOrName(i, genderIdorName);
                genderGridView.DataSource = genderManagers;
                genderGridView.DataBind();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void searchByIdButton_Click(object sender, EventArgs e)
        {
            int i = 0;
            LoadGridViewOnSaerch(i, searchIdTextBox.Value);
        }
        protected void searchByNameButton_Click(object sender, EventArgs e)
        {
            int i = 1;
            LoadGridViewOnSaerch(i, searchNameTextBox.Value);
        }
        protected void storeGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            genderGridView.PageIndex = e.NewPageIndex;
            LoadGridview();
        }

    }
}